package App.Controller;

import App.Model.*;
import App.Model.Account.*;
import App.Model.SMS.SMSCheckCodeRequest;
import App.Model.SMS.SMSCheckPhoneRequest;
import App.Model.SMS.SMSCheckPhoneResponse;
import App.Model.SMS.SMSGetCodeRequest;
import App.Service.*;
import io.lettuce.core.dynamic.annotation.Param;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by xw on 2018/11/12.
 */
@RestController
@RequestMapping("/api/account/")
public class AccountController extends BaseController {

    @Resource
    private AccountService _accountService;

    //   @Autowired
    //  private JwtManager jwtConfig;

    @Autowired
    private SMSService _smsService;

    @Autowired
    private QRCodeServcie _qrcodeService;


    @Autowired
    private XingeService _xingeService;


    /**
     * 极光一键登陆 zxt 20210916
     *
     * @param _request
     * @return
     */
    @RequestMapping(value = "key/login", method = RequestMethod.POST)
    public ResponseEntity<LoginResponse> KeyLogin(@RequestBody(required = false) LoginRequest _request) {

        // if(headers!=null){

        // headers.get()
        // }

        if (_request == null) {
            BaseResponse _baseResponse = ResponseError(null, "参数格式错误");
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }
        Object result = null;
        try {
            result = _accountService.keyLogin(_request);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ResponseEntity(result, HttpStatus.OK);
    }

    /**
     * 根据手机号获取token zxt 20210916
     *
     * @return
     */
    @RequestMapping(value = "get/token", method = RequestMethod.POST)
    public ResponseEntity<LoginResponse> getToken(@RequestBody(required = false) LoginRequest _request) {

        if (_request == null) {
            BaseResponse _baseResponse = ResponseError(null, "参数格式错误");
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }
        Object result = null;
        try {
            result = _accountService.getToken(_request.getUsername());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ResponseEntity(result, HttpStatus.OK);
    }


    /**
     * 注销
     *
     * @return
     */
    @RequestMapping(value = "cancellation", method = RequestMethod.POST)
    @ApiOperation("注销")
    public ResponseEntity cancellation(HttpServletRequest request, HttpServletResponse response) {
        BaseResponse _baseResponse = new BaseResponse();
        int _success = BaseEnum.ApiResult.Failure.getApiResult();
        _baseResponse.setSuccess(_success);
        _baseResponse.setMessage("操作成功");
        _accountService.cancellation(userId());
        return new ResponseEntity(_baseResponse, HttpStatus.OK);
    }

    //, @RequestHeader HttpHeaders headers
    @RequestMapping(value = "login", method = RequestMethod.POST)
    public ResponseEntity<LoginResponse> login(@RequestBody(required = false) LoginRequest _request) {

        // if(headers!=null){

        // headers.get()
        // }

        if (_request == null) {
            BaseResponse _baseResponse = ResponseError(null, "参数格式错误");
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }
        Object result = null;
        try {
            result = _accountService.login(_request);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ResponseEntity(result, HttpStatus.OK);
    }


    @RequestMapping("logout")
    public String logoutPage(HttpServletRequest request, HttpServletResponse response) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if (auth != null) {
            new SecurityContextLogoutHandler().logout(request, response, auth);

            String _authorization = request.getHeader("Authorization");
            String _token = _authorization.replace("Bearer ", "");
            _accountService.UpdateTokenInvalid(_token);

        }
        return "redirect:/login";
    }

    //注册用户
    @RequestMapping("register")
    public ResponseEntity<BaseResponse> register(@RequestBody(required = false) RegisterRequest _request) {

        if (_request == null) {
            BaseResponse _baseResponse = ResponseError(null, "参数格式错误");
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }
        BaseResponse _baseResponse = new BaseResponse();

        LoginRequest _loginRequset = new LoginRequest();
        _loginRequset.setUsername(_request.getUsername());

        CrmUser currentUser = _accountService.selectUser(_loginRequset);
        if (currentUser != null) {
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());
            _baseResponse.setMessage("注册失败，用户名已经存在");
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }

        int result = _accountService.register(_request);
        if (result > 0) {
            _baseResponse.setSuccess(BaseEnum.ApiResult.Success.getApiResult());
            _baseResponse.setMessage("注册成功");
        } else {
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());
            _baseResponse.setMessage("注册失败");
        }
        return new ResponseEntity(_baseResponse, HttpStatus.OK);
    }

    @RequestMapping("select")
    public ResponseEntity<?> select() {
        DataResponse _dataResponse = new DataResponse();
        try {
            List<CrmUser> users = _accountService.select();
            _dataResponse.setData(users);
        } catch (Exception ex) {
            return null;
        }
        return new ResponseEntity(_dataResponse, HttpStatus.OK);
    }


    @RequestMapping("getcode")
    public ResponseEntity<?> sendSms(@RequestBody(required = false) SMSGetCodeRequest _request) {
        BaseResponse _baseResponse = ResponseError(null, "");
        if (_request == null) {
            _baseResponse.setMessage("参数格式错误");
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }
        try {
            boolean _result = _smsService.sendSms(_request);

            if (_result) {
                _baseResponse.setMessage("操作成功");
                _baseResponse.setSuccess(BaseEnum.ApiResult.Success.getApiResult());
            } else {
                _baseResponse.setMessage("操作失败");
                _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());

            }
        } catch (Exception ex) {
            System.out.println("Exception:" + ex.getMessage());
            ex.printStackTrace();
        }
        return new ResponseEntity(_baseResponse, HttpStatus.OK);
    }

    //check code

    @RequestMapping("checkcode")
    public ResponseEntity<?> checkCode(@RequestBody(required = false) SMSCheckCodeRequest _request) {
        BaseResponse _baseResponse = ResponseError(null, "");
        if (_request == null) {
            _baseResponse.setMessage("参数格式错误");
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }
        try {
            boolean _result = _smsService.checkCode(_request);

            if (_result) {
                _baseResponse.setMessage("操作成功");
                _baseResponse.setSuccess(BaseEnum.ApiResult.Success.getApiResult());
            } else {
                _baseResponse.setMessage("操作失败");
                _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());

            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ResponseEntity(_baseResponse, HttpStatus.OK);
    }


    //检查手机号是否已经注册
    @RequestMapping("checkphone")
    public ResponseEntity<?> checkPhone(@RequestBody(required = false) SMSCheckPhoneRequest _request) {
        BaseResponse _baseResponse = ResponseError(null, "");
        if (_request == null) {
            _baseResponse.setMessage("参数格式错误");
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }
        try {

            //检查手机号是否为空
            if (_request.getPhone().isEmpty()) {
                _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());
                _baseResponse.setMessage("手机号不能为空");
            } else {

                boolean _result = _smsService.checkPhone(_request);
                SMSCheckPhoneResponse _checkResponse = new SMSCheckPhoneResponse();
                _checkResponse.setIsexist(_result);
                _baseResponse.setData(_checkResponse);
                _baseResponse.setMessage("操作成功");
                _baseResponse.setSuccess(BaseEnum.ApiResult.Success.getApiResult());
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ResponseEntity(_baseResponse, HttpStatus.OK);
    }


    //修改密码
    @RequestMapping(value = "changepwd", method = RequestMethod.POST)
    public ResponseEntity<?> changePassword(@RequestBody(required = false) ChagePasswordRequest _request) {
        BaseResponse _baseResponse = ResponseError(null, "");
        if (_request == null) {
            _baseResponse.setMessage("参数格式错误");
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }
        try {

            String _phone = _request.getPhone();
            String _newPassword = _request.getNewpassword();
            //  String _oldPassword = _request.getOldpassword();

            //检查手机号是否为空
            if (_phone.isEmpty()) {
                _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());
                _baseResponse.setMessage("手机号不能为空");
                return new ResponseEntity(_baseResponse, HttpStatus.OK);
            }
            /*else if (_oldPassword.isEmpty()) {
                _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());
                _baseResponse.setMessage("原来的密码不能为空");
                return new ResponseEntity(_baseResponse, HttpStatus.OK);
            }*/
            else if (_newPassword.isEmpty()) {
                _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());
                _baseResponse.setMessage("新的密码不能为空");
                return new ResponseEntity(_baseResponse, HttpStatus.OK);
            }
            /*else if (_newPassword.equals(_oldPassword)) {
                _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());
                _baseResponse.setMessage("新的密码和原来的密码不能相同");
                return new ResponseEntity(_baseResponse, HttpStatus.OK);
            }
            */
            else {
                int _result = _accountService.changePassword(_request);
                if (_result > 0) {
                    _baseResponse.setData("");
                    _baseResponse.setMessage("操作成功");
                    _baseResponse.setSuccess(BaseEnum.ApiResult.Success.getApiResult());
                    return new ResponseEntity(_baseResponse, HttpStatus.OK);
                } else if (_result == -1) {
                    _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());
                    _baseResponse.setMessage("用户名或者原来密码错误");
                    return new ResponseEntity(_baseResponse, HttpStatus.OK);
                } else {
                    _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());
                    _baseResponse.setMessage("修改密码失败");
                    return new ResponseEntity(_baseResponse, HttpStatus.OK);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());
            _baseResponse.setMessage(ex.getMessage());
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }
    }

    //更新用户信息
    @RequestMapping("updateuserinfo")
    public ResponseEntity<?> updateUserProfile(@RequestBody(required = false) UpdateUserInfoRequest _request) {
        BaseResponse _baseResponse = ResponseError(null, "");
        if (_request == null) {
            _baseResponse.setMessage("参数格式错误");
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }
        try {
            int _result = _accountService.updateUserProfile(_request, userId());
            if (_result > 0) {
                _baseResponse.setMessage("操作成功");
                _baseResponse.setSuccess(BaseEnum.ApiResult.Success.getApiResult());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ResponseEntity(_baseResponse, HttpStatus.OK);
    }


    //律师身份认证
    @RequestMapping("authuser")
    public ResponseEntity<?> authUser(@RequestBody(required = false) AuthUserRequest _request) {
        BaseResponse _baseResponse = ResponseError(null, "");
        if (_request == null) {
            _baseResponse.setMessage("参数格式错误");
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }
        try {
            int _result = _accountService.authUser(_request, userId());
            if (_result > 0) {
                _baseResponse.setMessage("操作成功");
                _baseResponse.setSuccess(BaseEnum.ApiResult.Success.getApiResult());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ResponseEntity(_baseResponse, HttpStatus.OK);
    }


    //获取律师业务专长
    @RequestMapping("expertises")
    public ResponseEntity<?> getLawyerExpertises() {
        DataResponse _dataResponse = new DataResponse();
        try {
            List<CrmDataItem> _result = _accountService.getLawyerExpertises();
            _dataResponse.setData(_result);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ResponseEntity(_dataResponse, HttpStatus.OK);
    }


    //律师认证的列表
    @RequestMapping("authuserlist")
    public ResponseEntity<?> getLawyerAuthList() {
        DataResponse _dataResponse = new DataResponse();
        try {

            List<AuthUserList> _result = _accountService.getLawyerAuthList(userId());
            _dataResponse.setData(_result);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ResponseEntity(_dataResponse, HttpStatus.OK);
    }


    //认证详情
    @RequestMapping("authuserdetail")
    public ResponseEntity<?> getLawyerAuthDetail() {
        DataResponse _dataResponse = new DataResponse();
        try {
            List<AuthUserList> _result = _accountService.getLawyerAuthList(userId());
            _dataResponse.setData(_result);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ResponseEntity(_dataResponse, HttpStatus.OK);
    }

    //认证详情
    @RequestMapping("authprocess")
    public ResponseEntity<?> getLawyerAuthProcess() {
        DataResponse _dataResponse = new DataResponse();
        try {
            AuthUserProcess _result = _accountService.getLawyerAuthProcess(userId());
            _dataResponse.setData(_result);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ResponseEntity(_dataResponse, HttpStatus.OK);
    }


    @RequestMapping("getuserdetail")
    public ResponseEntity<?> getUserDetail(@RequestBody(required = false) getUserDetailRequest _request) {
        DataResponse _dataResponse = new DataResponse();
        try {
            CrmUserDetail _result = _accountService.getUserDetail(_request);
            _dataResponse.setData(_result);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ResponseEntity(_dataResponse, HttpStatus.OK);
    }


    //认证详情
    @RequestMapping("getiminfo")
    public ResponseEntity<?> getTecentIMInfo() {
        DataResponse _dataResponse = new DataResponse();
        try {
            getIMInfoResponse _result = _accountService.getTencentIMInfo();
            _dataResponse.setData(_result);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ResponseEntity(_dataResponse, HttpStatus.OK);
    }


    /**
     * 生成二维码
     */
    @RequestMapping("getqrcode")
    public ResponseEntity<?> getQRCode(@RequestBody(required = false) WebLoginRequest _request) {
        DataResponse _dataResponse = new DataResponse();
        try {

            int _with = _request.getWidth();
            int _height = _request.getHeight();
            String _key = UUID.randomUUID().toString();
            _qrcodeService.generateQRCode(_key, _with, _height, "png", _key);

            WebLoginResponse _response = new WebLoginResponse();
            _response.setKey(_key);
            _response.setImgepath(_key + ".png");
            _dataResponse.setData(_response);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ResponseEntity(_dataResponse, HttpStatus.OK);
    }


    /**
     * 获取登陆token
     */
    @RequestMapping("gettoken")
    public ResponseEntity<?> geLogintToken(
            @RequestHeader(value = "version", required = false) String version,
            @RequestHeader(value = "device", required = false) String device,
            @RequestBody(required = false) WebLoginGetTokenRequest _request) {
        DataResponse _dataResponse = new DataResponse();
        try {
            String _key = _request.getKey();
            WebLoginGetTokenResponse _response = _qrcodeService.geLogintToken(_key);
            _dataResponse.setData(_response);

            int userId = _response.getUserid();
            String optMethod = "gettoken";
            if (version != null && device != null && userId > 0) {
                int clientTypeInt = BaseEnum.ClientType.getClientTypeStr(device);
                if (_response != null && _response.getToken() != null && _response.getToken() != "") {
                    _accountService.insertUserLoginHistory(userId, version,
                            clientTypeInt, BaseEnum.ResultType.Success.getResultType(), optMethod);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ResponseEntity(_dataResponse, HttpStatus.OK);
    }


    //从手机端，确认登陆
    @RequestMapping("confimweblogin")
    public ResponseEntity<BaseResponse> confirmWebLogin(
            @RequestHeader(value = "version", required = false) String version,
            @RequestHeader(value = "device", required = false) String device,
            @RequestBody(required = false) WebLoginConfirmRequest _request) {

        BaseResponse _baseResponse = ResponseError(null, "参数格式错误");
        if (_request == null) {
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }

        String _key = _request.getKey();
        String _token = _request.getToken();
        int _userid = _request.getUserid();
        int _result = _qrcodeService.updateToken(_key, _token, _userid);
        int resultType = BaseEnum.ResultType.Success.getResultType();
        if (_result > 0) {
            _baseResponse.setSuccess(BaseEnum.ApiResult.Success.getApiResult());
            _baseResponse.setMessage("操作成功");
            resultType = BaseEnum.ResultType.Success.getResultType();
        } else {
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());
            _baseResponse.setMessage("操作失败");
            resultType = BaseEnum.ResultType.Fail.getResultType();
        }

        String optMethod = "confimweblogin";
        if (version != null && device != null && _userid > 0) {
            int clientTypeInt = BaseEnum.ClientType.getClientTypeStr(device);
            _accountService.insertUserLoginHistory(_userid, version,
                    clientTypeInt, resultType, optMethod);
        }
        return new ResponseEntity(_baseResponse, HttpStatus.OK);
    }

    /**
     * 手机端扫码登录确定后轮询检测WEB端是否已登录进去，手机端端专用
     * @param version
     * @param device
     * @param _request
     * @return
     */
    @RequestMapping("checkWeblogined")
    public ResponseEntity<BaseResponse> checkWeblogined(
            @RequestHeader(value = "version", required = false) String version,
            @RequestHeader(value = "device", required = false) String device,
            @RequestBody(required = false) WebLoginConfirmRequest _request) {
        BaseResponse _baseResponse = ResponseError(null, "参数格式错误");
        if (_request == null) {
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }
        _baseResponse.setSuccess(BaseEnum.ApiResult.Success.getApiResult());
        _baseResponse.setMessage("操作成功");
        try {
            String _key = _request.getKey();
            int _userid = _request.getUserid();
            List<CrmWebLogin> weblogin=_qrcodeService.getToken(_key);
            //说明手机端已确定点击过登录
            if(weblogin!=null&weblogin.size()!=0){
                    Date createtime=weblogin.get(0).getCreatetime();//点击时间
                if(createtime!=null){
                    Date logincreate=_accountService.GetWebLoginTime(_userid);//web端登录进去时间
                    //当web登录时间为空或登录时小于手机点击确定时间时表示web端还没有登录
                    System.out.println(createtime);
                    System.out.println(createtime.getTime());
                    System.out.println(logincreate);
                    //System.out.println(logincreate.getTime());

                        if(logincreate==null||createtime.getTime()>logincreate.getTime()){
                            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());
                            _baseResponse.setMessage("操作失败");
                        }

                    }else {
                    _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());
                    _baseResponse.setMessage("操作失败");
                }
            }else {
                _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());
                _baseResponse.setMessage("操作失败");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());
            _baseResponse.setMessage("操作失败");
        }
          return new ResponseEntity(_baseResponse, HttpStatus.OK);
    }


    //推送重新登陆请求
    @RequestMapping("pushweblogin")
    public ResponseEntity<BaseResponse> pushWebLogin(@RequestBody(required = false) PushWebloginRequest _request) {

        BaseResponse _baseResponse = ResponseError(null, "参数格式错误");
        if (_request == null) {
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }
        try {
            String _key = _request.getKey();
            int _userid = _request.getUserid();
            boolean _result = _qrcodeService.pushWebLogin(_key, _userid);
            if (_result) {
                _baseResponse.setSuccess(BaseEnum.ApiResult.Success.getApiResult());
                _baseResponse.setMessage("操作成功");
            } else {
                _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());
                _baseResponse.setMessage("操作失败");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ResponseEntity(_baseResponse, HttpStatus.OK);
    }


    //重新确认web登陆
    @RequestMapping("confirmrelogin")
    public ResponseEntity<BaseResponse> confirmReWebLogin(
            @RequestHeader(value = "version", required = false) String version,
            @RequestHeader(value = "device", required = false) String device,
            @RequestBody(required = false) ConfirmReWebLoginRequest _request) {

        BaseResponse _baseResponse = ResponseError(null, "参数格式错误");
        if (_request == null) {
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }

        int resultType = BaseEnum.ResultType.Success.getResultType();
        String _key = _request.getKey();
        int _result = _qrcodeService.confirmReWebLogin(_key);
        if (_result > 0) {
            _baseResponse.setSuccess(BaseEnum.ApiResult.Success.getApiResult());
            _baseResponse.setMessage("操作成功");
            resultType = BaseEnum.ResultType.Success.getResultType();
        } else {
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());
            _baseResponse.setMessage("操作失败");
            resultType = BaseEnum.ResultType.Fail.getResultType();
        }

        String optMethod = "confirmrelogin";
        CrmWebLogin user = _qrcodeService.getWebReLoginConfirmed(_key);
        if (version != null && device != null && user != null) {
            int clientTypeInt = BaseEnum.ClientType.getClientTypeStr(device);
            _accountService.insertUserLoginHistory(user.getUserid(), version,
                    clientTypeInt, resultType, optMethod);
        }

        return new ResponseEntity(_baseResponse, HttpStatus.OK);
    }


    //重新登陆是否已经确认
    @RequestMapping("isconfirmed")
    public ResponseEntity<BaseResponse> isWebReLoginConfirmed(
            @RequestHeader(value = "version", required = false) String version,
            @RequestHeader(value = "device", required = false) String device,
            @RequestBody(required = false) ConfirmReWebLoginRequest _request) {

        BaseResponse _baseResponse = ResponseError(null, "参数格式错误");
        if (_request == null) {
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }

        String _key = _request.getKey();
        boolean _result = _qrcodeService.isWebReLoginConfirmed(_key);

        ConfirmReWebLoginResponse _response = new ConfirmReWebLoginResponse();
        _response.setIsconfirmed(_result);

        if (_result) {
            _baseResponse.setSuccess(BaseEnum.ApiResult.Success.getApiResult());
            _baseResponse.setMessage("操作成功");
            _baseResponse.setData(_response);

            String optMethod = "isconfirmed";
            int resultType = BaseEnum.ResultType.Success.getResultType();
            CrmWebLogin user = _qrcodeService.getWebReLoginConfirmed(_key);
            if (version != null && device != null && user != null) {
                int clientTypeInt = BaseEnum.ClientType.getClientTypeStr(device);
                _accountService.insertUserLoginHistory(user.getUserid(), version,
                        clientTypeInt, resultType, optMethod);
            }
        } else {
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());
            _baseResponse.setMessage("操作失败");
            _baseResponse.setData(_response);
        }
        return new ResponseEntity(_baseResponse, HttpStatus.OK);
    }


    @RequestMapping("sendmessage")
    public ResponseEntity<?> sendGroupMessage(@RequestBody(required = false) SendGroupMessageRequest _request) {
        DataResponse _dataResponse = new DataResponse();
        try {

            //   String  _result= _xingeService.sendGrooupMessage(_request.groupid, _request.content, userId());
            List<String> _userIds = new ArrayList<>();
            _userIds.add(Integer.toString(107));

            Date _now = new Date();

            SimpleDateFormat _format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String _result = _xingeService.pushMessage("", "", _userIds, "push tile", "who are you?:" + _format.format(_now));

            _dataResponse.setData(_result);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ResponseEntity(_dataResponse, HttpStatus.OK);
    }


    @RequestMapping("sendcommonmessage")
    public ResponseEntity<?> sendGroupCommonMessage(@RequestBody(required = false) SendGroupMessageRequest _request) {
        DataResponse _dataResponse = new DataResponse();
        try {
            String _result = "";//_tecent.sendGrooupCommonMessage(_request.groupid, _request.content, userId());

            _dataResponse.setData(_result);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ResponseEntity(_dataResponse, HttpStatus.OK);
    }


    //登陆过之后获取新token H5专用
    @RequestMapping("getnewtoken")
    public ResponseEntity<?> geNewToken(@RequestHeader(value = "Authorization") String userAuth) {
        DataResponse _dataResponse = new DataResponse();
        try {

            String _userAuth = userAuth;

            GetNewTokenResponse _result = _accountService.getNewToken(_userAuth);
            _dataResponse.setData(_result);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ResponseEntity(_dataResponse, HttpStatus.OK);
    }

    //登陆过之后获取新token Mobile专用 解决token失效后可以自动登录
    @RequestMapping("getmobilenewtoken")
    public ResponseEntity<?> geMobileNewToken(@RequestHeader(value = "Authorization") String userAuth) {
        DataResponse _dataResponse = new DataResponse();
        try {
            String _userAuth = userAuth;
            GetNewTokenResponse _result = _accountService.getMobileNewToken(_userAuth);
            _dataResponse.setData(_result);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ResponseEntity(_dataResponse, HttpStatus.OK);
    }

    @RequestMapping("checkcodelogin")
    public ResponseEntity<?> checkCodeLogin(@RequestHeader(value = "version", required = false) String version,
                                            @RequestHeader(value = "device", required = false) String device,
                                            @RequestBody(required = false) SMSCheckCodeRequest _request) {
        BaseResponse _baseResponse = ResponseError(null, "");
        if (_request == null) {
            _baseResponse.setMessage("参数格式错误");
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }

        Object result = null;
        try {
            boolean _result = _smsService.checkCode(_request);
            if (_result) {
                LoginRequest _requestLogin = new LoginRequest();
                _requestLogin.setUsername(_request.getPhone());
                result = _accountService.loginForCheckCode(_requestLogin);
                if (version != null && device != null) {
                    int userId = ((BaseResponse) result).getUserid();
                    int clientTypeInt = BaseEnum.ClientType.getClientTypeStr(device);
                    String optMethod = "checkcodelogin";
                    if (result != null && ((BaseResponse) result).getSuccess() > 0) {
                        _accountService.insertUserLoginHistory(userId, version,
                                clientTypeInt, BaseEnum.ResultType.Success.getResultType(), optMethod);
                    } else {
                        _accountService.insertUserLoginHistory(userId, version,
                                clientTypeInt, BaseEnum.ResultType.Fail.getResultType(), optMethod);
                    }
                }
            } else {
                BaseResponse _response = new BaseResponse();
                _response.setMessage("验证码错误");
                result = _response;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ResponseEntity(result, HttpStatus.OK);
    }

    // 2022/4/21 检查token是否可用
    @RequestMapping("checktoken")
    public ResponseEntity<?> checkToken(@RequestBody CheckTokenRequest _request) {
        boolean result = _accountService.checkToken(_request);
        Map<String, Object> map = new HashMap<>();
        map.put("token", _request.getToken());
        map.put("isValid", result);
        BaseResponse _response = new BaseResponse();
        _response.setData(map);
        if (result) {
            _response.setMessage("有效的token");
        } else {
            _response.setMessage("当前token已无效");
        }
        return new ResponseEntity(_response, HttpStatus.OK);
    }

    // 2022/10/27  商城通过手机号获取token
    @PostMapping("shoptoken")
    public ResponseEntity<?> shopGetToken(@RequestBody SMSCheckCodeRequest _request) {
        BaseResponse _response = new BaseResponse();
        try {
            boolean _result = _smsService.checkCode(_request);
            if (_result) {
                String token = _accountService.shopGetToken(_request.getPhone());
                Map<String, Object> map = new HashMap<>();
                if (null != token) {
                    map.put("token", "Bearer " + token);
                    map.put("isLogin", true);
                    _response.setData(map);
                    _response.setMessage("操作成功");
                } else {
                    map.put("token", "");
                    map.put("isLogin", false);
                    _response.setData(map);
                    _response.setSuccess(401);
                    _response.setMessage("请登录后操作");
                }
            } else {
                _response.setMessage("验证码错误");
                _response.setSuccess(203);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ResponseEntity(_response, HttpStatus.OK);
    }

    // 2022/4/21 根据最后token获取新token,解决自动登录
    @RequestMapping("tokenbytoken")
    public ResponseEntity<?> getTokenByOldToken(@RequestHeader(value = "Authorization") String token) {
        BaseResponse _response = new BaseResponse();
        try {
            String newToken = _accountService.getTokenByOldToken(token);
            Map<String, Object> map = new HashMap<>();
            if (null != newToken) {
                map.put("token", newToken);
                map.put("isLogin", true);
                _response.setData(map);
                _response.setMessage("操作成功");
            } else {
                map.put("token", "");
                map.put("isLogin", false);
                _response.setData(map);
                _response.setMessage("请登录后操作");
                _response.setSuccess(401);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ResponseEntity(_response, HttpStatus.OK);
    }
    //end of class
}
